const router = require('koa-router')();
const checkPermission = require('../middleware/checkPermission');
const errorHandler = require('../middleware/errorHandler');
const { loginByBackMobileCode } = require('../../../service/api4back');
const { getSingleActivitySettingInfo } = require('../../../service/yjxMysql');
const { formatSuccessJson, formatErrorJson } = require('../../../utils/retUtil');
const CONST = require('../../../utils/const');
const promoter = require('./promoter/promoter');
const users = require('./promoter/users');


router.prefix('/yjx/v1');
router.use(errorHandler);
router.use(checkPermission);

router.use(promoter.routes(), promoter.allowedMethods());
router.use(users.routes(), users.allowedMethods());

router.get('/', async (ctx, next) => {
    await ctx.render('index', {
        title: 'Hello Koa 2! by yjx/v1'
    })
});

router.get('/string', async (ctx, next) => {
    ctx.body = 'koa2 string by yjx/v1'
});

router.get('/json', async (ctx, next) => {
    console.log('yjx/v1/json');
    ctx.body = {
        title: 'koa2 json by yjx/v1'
    };

    console.log('session-users: ' + ctx.session[CONST.SESSION_USER]);
    const result = await loginByBackMobileCode({
        zone: '+86',
        mobile: '18256276313',
        verCode: '123456'
    });

    // const result = await getSingleActivitySettingInfo(16);
    ctx.session[CONST.SESSION_USER] = result;
    ctx.body = formatSuccessJson(result);
});

module.exports = router;
